package cn.rtplay.esp32camcar;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

public class UserData {
    public static final String PREFERENCES_SETTING_DIRECTION_CONTROL_MODE = "direction_control";
    public static final String PREFERENCES_SETTING_STEERING_WHEEL_STYLE = "wheel_style";
    public static final String PREFERENCES_SETTING_GEAR_CONTROL_MODE = "control_mode";
    public static final String PREFERENCES_SETTING_IMAGE_QUALITY = "image_quality";
    public static final String PREFERENCES_SETTING_SOUND = "sound";
    public static final String PREFERENCES_SETTING_VOLUME = "volume";
    public static final String PREFERENCES_SETTING_UI_COLOR = "ui_color";
    public static final String PREFERENCES_SETTING_CORRECTION_ANGLE = "correction_angle";

    public static final int VALUE_DIRECTION_CONTROL_MODE_SENSOR = 0;
    public static final int VALUE_DIRECTION_CONTROL_MODE_MANUAL = 1;

    public static final int VALUE_STEERING_WHEEL_STYLE_1 = 0;
    public static final int VALUE_STEERING_WHEEL_STYLE_2 = 1;

    public static final int VALUE_GEAR_CONTROL_MODE_AUTO = 0;
    public static final int VALUE_GEAR_CONTROL_MODE_MANUAL = 1;

    public static final int VALUE_IMAGE_QUALITY_SMOOTH = 0;
    public static final int VALUE_IMAGE_QUALITY_STANDARD = 1;
    public static final int VALUE_IMAGE_QUALITY_HD = 2;

    public static final int VALUE_SOUND_CLOSE = 0;
    public static final int VALUE_SOUND_OPEN = 1;

    public static final int VALUE_UI_COLOR_BLACK = 0;
    public static final int VALUE_UI_COLOR_GREY = 1;
    public static final int VALUE_UI_COLOR_DARKSLATEGRAY = 2;
    public static final int VALUE_UI_COLOR_MIDNIGHTBLUE = 3;
    public static final int VALUE_UI_COLOR_SLATEBLUE = 4;
    public static final int VALUE_UI_COLOR_SIENNA = 5;

    private final SharedPreferences mSharedPreferences;
    private int mDirectionControlMode;
    private int mSteeringWheelStyle;
    private int mGearControlMode;
    private int mImageQuality;
    private int mSoundSetting;
    private int mVolumeSetting;
    private int mUIColor;
    private int mCorrectionAngle;

    public UserData(Context context) {
        String PREFERENCES_SETTING = "setting";
        mSharedPreferences = context.getSharedPreferences(PREFERENCES_SETTING, MODE_PRIVATE);
        updateUserSetting();
    }

    /**
     * 获取用户设置
     */
    public void updateUserSetting() {
        mDirectionControlMode = mSharedPreferences.getInt(PREFERENCES_SETTING_DIRECTION_CONTROL_MODE, VALUE_DIRECTION_CONTROL_MODE_MANUAL);
        mSteeringWheelStyle = mSharedPreferences.getInt(PREFERENCES_SETTING_STEERING_WHEEL_STYLE, VALUE_STEERING_WHEEL_STYLE_1);
        mGearControlMode = mSharedPreferences.getInt(PREFERENCES_SETTING_GEAR_CONTROL_MODE, VALUE_GEAR_CONTROL_MODE_AUTO);
        mImageQuality = mSharedPreferences.getInt(PREFERENCES_SETTING_IMAGE_QUALITY, VALUE_IMAGE_QUALITY_STANDARD);
        mSoundSetting = mSharedPreferences.getInt(PREFERENCES_SETTING_SOUND, VALUE_SOUND_OPEN);
        mVolumeSetting = mSharedPreferences.getInt(PREFERENCES_SETTING_VOLUME, 15);
        mUIColor = mSharedPreferences.getInt(PREFERENCES_SETTING_UI_COLOR, VALUE_UI_COLOR_BLACK);
        mCorrectionAngle = mSharedPreferences.getInt(PREFERENCES_SETTING_CORRECTION_ANGLE, 0);
    }

    /**
     *
     * @param ele 需要更新的键值
     * @return 查询后的值
     */
    public int updateUserSetting(String ele) {
        int ret;
        switch (ele) {
            case PREFERENCES_SETTING_DIRECTION_CONTROL_MODE:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_DIRECTION_CONTROL_MODE, VALUE_DIRECTION_CONTROL_MODE_SENSOR);
                break;
            case PREFERENCES_SETTING_STEERING_WHEEL_STYLE:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_STEERING_WHEEL_STYLE, VALUE_STEERING_WHEEL_STYLE_1);
                break;
            case PREFERENCES_SETTING_GEAR_CONTROL_MODE:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_GEAR_CONTROL_MODE, VALUE_GEAR_CONTROL_MODE_AUTO);
                break;
            case PREFERENCES_SETTING_IMAGE_QUALITY:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_IMAGE_QUALITY, VALUE_IMAGE_QUALITY_STANDARD);
                break;
            case PREFERENCES_SETTING_SOUND:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_SOUND, VALUE_SOUND_OPEN);
                break;
            case PREFERENCES_SETTING_VOLUME:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_VOLUME, 15);
                break;
            case PREFERENCES_SETTING_UI_COLOR:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_UI_COLOR, VALUE_UI_COLOR_BLACK);
                break;
            case PREFERENCES_SETTING_CORRECTION_ANGLE:
                ret = mSharedPreferences.getInt(PREFERENCES_SETTING_CORRECTION_ANGLE, 0);
                break;
            default:
                ret = 0;
        }
        return ret;
    }

    public int getUserSetting(String ele) {
        switch (ele) {
            case PREFERENCES_SETTING_DIRECTION_CONTROL_MODE:
                return mDirectionControlMode;
            case PREFERENCES_SETTING_STEERING_WHEEL_STYLE:
                return mSteeringWheelStyle;
            case PREFERENCES_SETTING_GEAR_CONTROL_MODE:
                return mGearControlMode;
            case PREFERENCES_SETTING_IMAGE_QUALITY:
                return mImageQuality;
            case PREFERENCES_SETTING_SOUND:
                return mSoundSetting;
            case PREFERENCES_SETTING_VOLUME:
                return mVolumeSetting;
            case PREFERENCES_SETTING_UI_COLOR:
                return mUIColor;
            case PREFERENCES_SETTING_CORRECTION_ANGLE:
                return mCorrectionAngle;
            default:
                return 0;
        }
    }

    /**
     *
     * @param ele 需要更新的键值
     * @param value 设置值
     */
    public void setUserSetting(String ele, int value) {
        switch (ele) {
            case PREFERENCES_SETTING_DIRECTION_CONTROL_MODE:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_DIRECTION_CONTROL_MODE, value).apply();
                mDirectionControlMode = value;
                break;
            case PREFERENCES_SETTING_STEERING_WHEEL_STYLE:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_STEERING_WHEEL_STYLE, value).apply();
                mSteeringWheelStyle = value;
                break;
            case PREFERENCES_SETTING_GEAR_CONTROL_MODE:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_GEAR_CONTROL_MODE, value).apply();
                mGearControlMode = value;
                break;
            case PREFERENCES_SETTING_IMAGE_QUALITY:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_IMAGE_QUALITY, value).apply();
                mImageQuality = value;
                break;
            case PREFERENCES_SETTING_SOUND:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_SOUND, value).apply();
                mSoundSetting = value;
                break;
            case PREFERENCES_SETTING_VOLUME:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_VOLUME, value).apply();
                mVolumeSetting = value;
                break;
            case PREFERENCES_SETTING_UI_COLOR:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_UI_COLOR, value).apply();
                mUIColor = value;
                break;
            case PREFERENCES_SETTING_CORRECTION_ANGLE:
                mSharedPreferences.edit().putInt(PREFERENCES_SETTING_CORRECTION_ANGLE, value).apply();
                mCorrectionAngle = value;
                break;
        }
    }
}
